package com.t.e.leaksdemo;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.widget.TextView;

import com.squareup.leakcanary.LeakCanary;
import com.squareup.leakcanary.RefWatcher;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;



public class LeaksActivity extends Activity {

    public TextView tv;

//    @Override
//    protected void onDestroy() {
//        super.onDestroy();
//        RefWatcher refWatcher = LeaksDemoApp.getRefWatcher(this);
//        refWatcher.watch(this);
//        System.out.println("LeaksActivity:onDestroy");
//    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_leaks);
//        outOfMemoryTest();
        //这个tv会泄漏，本activity结束后，仍有TestDataModel指向它，所以它是本activity产生的泄漏
        tv = (TextView) findViewById(R.id.tv_leak_aty);
        ActivityManager activityManager = (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE);
        ActivityManager.MemoryInfo info = new ActivityManager.MemoryInfo();
        activityManager.getMemoryInfo(info);
        tv.setText("meminfo 系统剩余内存:" + (info.availMem >> 10) + "k" + "\n");
        tv.append("系统是否处于低内存运行：" + info.lowMemory + "\n");
        tv.append("当系统剩余内存低于" + (info.threshold >> 10) + "k" + "时就看成低内存运行" + "\n");

        tv.append("本Activity有内存泄漏代码，重复进入本activity，旋转屏幕可暴漏");
        TestDataModel.getInstance().setRetainedTextView(tv);
    }

    @Override
    public void onTrimMemory(int level) {
        super.onTrimMemory(level);
    }

    public class Pilot{
        String name;
        int age;

        public Pilot(String a, int b){
            name = a;
            age = b;
        }
    }
    private void outOfMemoryTest() {
        Map<String, Pilot> map = new HashMap<String, Pilot>();
        Object[] array = new Object[1000000];
        for (int i = 0; i < 1000000; i++) {
            String d = new Date().toString();
            Pilot p = new Pilot(d, i);
            map.put(i + "rosen jiang", p);
            array[i] = p;
        }
    }
}
